package cn.gok.oa.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
    public static Date StringToDate(String str) throws ParseException {
        Date date;
        if (str != null && str != ""){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = (Date) formatter.parse(str);
        }else{
            date = null;
        }

        return date;
    }
    public static LocalDateTime dateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }
    public static LocalTime localDateTimeToLocalTime(LocalDateTime localDateTime) {
        return localDateTime.toLocalTime();
    }
    public static List<LocalDate> getMonthDay(String year,String month){
        LocalDate date = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month),1);
        LocalDate lastMonth = date.minusMonths(0); // 当前月份减1
        LocalDate firstDay = lastMonth.with(TemporalAdjusters.firstDayOfMonth()); // 获取当前月的第一天
        LocalDate lastDay = lastMonth.with(TemporalAdjusters.lastDayOfMonth()); // 获取当前月的最后一天
        List<LocalDate> strings = new ArrayList<>();
        strings.add(0, firstDay);
        strings.add(1,lastDay);
        return strings;
    }
    public static Date LocalDateToDate(LocalDate localDate) {
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        return date;
    }
}
